package org.example.music_manage_system.controller;

import org.example.music_manage_system.pojo.Category;
import org.example.music_manage_system.pojo.Music;
import org.example.music_manage_system.pojo.Result;
import org.example.music_manage_system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
//增加音乐分类
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success();
    }
    // 查询所有音乐分类
//    及时更改Authorization，不同用户的token不一样，错误使用会导致无法获取用户信息以及外键约束无法插入数据
    @GetMapping
    public Result<List<Category>> List(){
        List<Category> list = categoryService.List();
        return Result.success(list);
    }
//    查询歌曲分类
    @GetMapping("/detail")
    public Result<Category> ListDetails(Integer id){
        Category list = categoryService.findById(id);
        if(list == null){
            return Result.error("分类信息不存在");
        }
        return Result.success(list);
    }
//    更新分类
    @PutMapping
    public Result update(@RequestBody @Validated(Music.update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }
//    删除分类
    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }
}

